package com.yt.sys.common.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
 * @Description ：extends WebMvcConfigurerAdapter 已经被弃用，直接实现WebMvcConfigurer该接口就行了
 * @Author ： 解风
 * @Date ： 2017/11/28 09:41
 */
@Configuration
public class ApiWebConfig implements WebMvcConfigurer {


    @Autowired
    private ApiHandlerInterceptor apiHandlerInterceptor;

    /**
     * 注册拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(apiHandlerInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/v2/api-docs", "/images/**","/swagger-resources/**","/static/**"); // 排除拦截;
    }

    @Override
    public void addCorsMappings(CorsRegistry registry){
        registry.addMapping("/**/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT","PATCH")
                .maxAge(3600);
    }

}
